package com.hyt.it.ogt.kq.main.controller.gov;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.annotation.Progress;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.enums.ProgressEnum;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.param.CreateRoomDataParam;
import com.hyt.it.ogt.kq.service.gov.model.param.PageRoomPackageParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.RoomPackageVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.roomdata.CreateTimeRoomVO;
import com.hyt.it.ogt.kq.service.gov.service.IRoomDataService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomCandidateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 考务包管理接口
 * </p>
 *
 * @author liuhh
 * @since 2021-9-9
 */
@RestController
@Api(tags = "116.考务包管理接口", value = "考务包管理接口")
@ApiSort(value = 116)
@RequestMapping("/kw/gov/roomData")
@Slf4j
public class RoomDataController extends BaseController {

    @Resource
    private IRoomDataService iRoomDataService;

    @Resource
    private ITimeRoomCandidateService iTimeRoomCandidateService;

    @ApiOperation(
            value = "16.1 生成考务包",
            notes = "33001:任务id不能为空;33002:考务包已存在不允许重新生成，请先取消锁定编排后再生成考务包;" +
                    "30016:数据已锁定，不允许生成考务，请先解锁编排;30000:成功;")
    @ApiOperationSort(value = 1)
    @PostMapping(value = "create")
    @Progress(name = ProgressEnum.CREATE_ROOM_DATA)
    public ApiResponse<Object> create(@RequestBody CreateRoomDataParam param) {
        CreateTimeRoomVO data = CreateTimeRoomVO.builder().build();
        int code = ResponseCode.SUCCESS.getCode();
        try {
            iRoomDataService.createByTaskId(param.getTaskId(), null);
        } catch (KqException i) {
            log.error("# 不符合生成考务包条件: {}#{}", i.getErrCode(), i.getMessage());
            if (ResponseCode.EXIST_UN_ARRANGE_TIME_ROOM.getCode().intValue() == i.getErrCode()) {
                // 存在未编排考生考场，不允许生成考务包
                data.setData(i.getData());
                data.setType(1);
            } else {
                code = i.getErrCode();
            }
        } catch (Exception e) {
            log.error("# 生成考务包异常:", e);
            code = ResponseCode.ERROR_ROOM_DATA_CREATE.getCode();
        }
        return ApiResponse.builder()
                .code(code)
                .data(data)
                .build();
    }

    @ApiOperation(
            value = "16.2 分页查询考务包数据",
            notes = "32826:任务id不能为空;" +
                    "32827:查询异常;" +
                    "30000:成功;"
    )
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ApiResponse<Page<RoomPackageVO>> page(PageRoomPackageParam param) {
        try {
            Page<RoomPackageVO> page = iRoomDataService.page(param);
            return ApiResponse.<Page<RoomPackageVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(page)
                    .build();
        }catch (KqException kge) {
            log.error("# 生成考务包异常: {}", kge.getMessage());
            return ApiResponse.<Page<RoomPackageVO>>builder()
                    .code(kge.getErrCode())
                    .build();
        }  catch (Exception e) {
            log.error("# 查询考务包分页数据参数:", e);
            return ApiResponse.<Page<RoomPackageVO>>builder()
                    .code(ResponseCode.ERROR_ROOM_DATA_SELECT_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "16.3 生成dbf考务数据包"
    )
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/exportArrangeResultDBF", method = RequestMethod.GET)
    public ApiResponse<String> exportArrangeResultDBF(String taskId) {
        try {
            return ApiResponse.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTimeRoomCandidateService.exportArrangeResultDBF(taskId))
                    .build();
        }  catch (Exception e) {
            log.error("# 生成dbf考务数据包异常:", e);
            return ApiResponse.<String>builder().code(ResponseCode.ERROR_CREATE_DBF.getCode()).build();
        }
    }

}
